package com.cloudsigns.factorymanager2.produce.fragment.produce_team;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.customview.ListViewForScrollView;
import com.cloudsigns.factorymanager2.produce.adapter.produce_team.ProduceTeamOrderInformationAdapter;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartHandOverOrderBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartOrderBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartOrderResponse;
import com.cloudsigns.factorymanager2.produce.iview.produce_team.IAssignProductOrderReceiveView;
import com.cloudsigns.factorymanager2.produce.presenter.produce_team.AssignProductOrderReceivePresenter;
import com.cloudsigns.factorymanager2.produce.utils.ProduceForTeamEvent;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.List;

import de.greenrobot.event.EventBus;

public class ProduceTeamOrderInformationFragment extends Fragment implements View.OnClickListener, IAssignProductOrderReceiveView {
    private TextView tv_process_name, tv_product_name, tv_engineering_name;
    private ImageView img_arrow_down;
    private LinearLayout ll_content;
    private TextView edt_specifition_size,edt_slip_size, edt_process_num, edt_process_cycle, edt_dispatch_people, edt_dispath_time, edt_handover_status;
    private ListViewForScrollView lv_cycle_yield;
    private Button btn_receive_order, btn_return_order;

    private ProduceTeamOrderInformationAdapter adapter;

    private AssignProductOrderReceivePresenter assignProductOrderReceivePresenter;

    private boolean isClick = true;
    private CommonEnum.STATUS status;
    private WorkPartBean workPartBean;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_produce_team_order_information, null);
        tv_process_name = view.findViewById(R.id.tv_process_name);
        tv_product_name = view.findViewById(R.id.tv_product_name);
        tv_engineering_name = view.findViewById(R.id.tv_engineering_name);
        img_arrow_down = view.findViewById(R.id.img_arrow_down);
        ll_content = view.findViewById(R.id.ll_content);
        edt_specifition_size = view.findViewById(R.id.edt_specifition_size);
        edt_slip_size=view.findViewById(R.id.edt_slip_size);
        edt_process_num = view.findViewById(R.id.edt_process_num);
        edt_process_cycle = view.findViewById(R.id.edt_process_cycle);
        edt_dispatch_people = view.findViewById(R.id.edt_dispatch_people);
        edt_dispath_time = view.findViewById(R.id.edt_dispath_time);
        edt_handover_status = view.findViewById(R.id.edt_handover_status);
        lv_cycle_yield = view.findViewById(R.id.lv_cycle_yield);
        btn_receive_order = view.findViewById(R.id.btn_receive_order);
        btn_return_order = view.findViewById(R.id.btn_return_order);

        img_arrow_down.setOnClickListener(this);
        btn_receive_order.setOnClickListener(this);
        btn_return_order.setOnClickListener(this);

        img_arrow_down.setBackgroundResource(R.mipmap.arrow_down_icon);

        return view;
    }

    private WorkPartOrderBean workPartOrderBean;

    public void initData(WorkPartBean workPartBean, WorkPartOrderResponse workPartOrderResponse) {
        if (null != workPartBean) {
            this.workPartBean = workPartBean;
            this.workPartOrderBean = this.workPartBean.getWorkPartOrderById(workPartOrderResponse.getWorkPartOrderId());
            tv_process_name.setText(workPartBean.getWorkPartName());
            tv_product_name.setText(" / " + workPartBean.getProductName());
            tv_engineering_name.setText(" / " + workPartBean.getEngineeringName());
            edt_process_cycle.setText(DateUtil.getYyyy_Mm_Dd(workPartBean.getStartDate())+" ~ "+DateUtil.getYyyy_Mm_Dd(workPartBean.getCompleteDate()));

            CommonEnum.ProductShapeType shape = workPartBean.getShapeType().getType();
            if (null != shape) {
                if (shape == CommonEnum.ProductShapeType.ROUND) {
                    edt_specifition_size.setText("直径" + workPartBean.getShapeType().getDiameter() + "mm*" + "厚度" + workPartBean.getShapeType().getThick() + "mm");
                } else if (shape == CommonEnum.ProductShapeType.RECTANGLE) {
                    edt_specifition_size.setText("长度" + workPartBean.getShapeType().getWidth() + "mm*" + "高度" + workPartBean.getShapeType().getHeight() + "mm*" + "厚度" + workPartBean.getShapeType().getThick() + "mm");
                } else if (shape == CommonEnum.ProductShapeType.SPECIALSHAPE) {
                    edt_specifition_size.setText(workPartBean.getShapeType().getDesc());
                }
            }

            edt_slip_size.setText("上" + workPartBean.getShapeType().getBorderTop() + "mm*" + "下" + workPartBean.getShapeType().getBorderBottom() + "mm*" + "左" + workPartBean.getShapeType().getBorderLeft() + "mm*" + "右" + workPartBean.getShapeType().getBorderRight() + "mm");

            if (workPartOrderBean != null) {
                edt_process_num.setText(workPartOrderBean.getAmount() + workPartBean.getUnit());
                edt_dispatch_people.setText(workPartOrderBean.getAssignAccountName());
                edt_dispath_time.setText(DateUtil.getYyyy_Mm_Dd(workPartOrderBean.getSendAssignOrderTime()));

//                String assignReceiveAccountId = workPartOrderBean.getAssignReceiveAccountId();
//                if (null != assignReceiveAccountId || !TextUtils.isEmpty(assignReceiveAccountId)) {
//                    btn_receive_order.setVisibility(View.GONE);
//                    btn_return_order.setVisibility(View.GONE);
//                }

                CommonEnum.ProduceOrderStatus assignReceiveStatus= workPartOrderBean.getAssignReceiveStatus();
                if (assignReceiveStatus==CommonEnum.ProduceOrderStatus.RECEIVED||assignReceiveStatus==CommonEnum.ProduceOrderStatus.RETURNED){
                    btn_receive_order.setVisibility(View.GONE);
                    btn_return_order.setVisibility(View.GONE);
                }else{
                    btn_receive_order.setVisibility(View.VISIBLE);
                    btn_return_order.setVisibility(View.VISIBLE);
                }

                List<WorkPartHandOverOrderBean> workPartHandOverOrderBeanList = workPartOrderBean.getWorkPartHandOverOrderList();
                String strHandOver = "";
                for (WorkPartHandOverOrderBean workPartHandOverOrderBean : workPartHandOverOrderBeanList) {
                    strHandOver += workPartHandOverOrderBean.getDescript(workPartBean.getUnit()) + ";";
                }
                edt_handover_status.setText(strHandOver);
            }


            adapter = new ProduceTeamOrderInformationAdapter(getActivity());
            adapter.setStrUnit(workPartBean.getUnit());
            adapter.setData(workPartBean.getWorkPartScheduleList());
            lv_cycle_yield.setAdapter(adapter);

            assignProductOrderReceivePresenter = new AssignProductOrderReceivePresenter(this);
        }
    }

    // 接收
    private void receiveOrder() {
        status = CommonEnum.STATUS.RECEIVED;
        isClick = false;
        btnClickable();
        assignProductOrderReceivePresenter.assignProductOrderReceive();
    }

    // 退回
    private void returnOrder() {
        status = CommonEnum.STATUS.RETURNED;
        isClick = false;
        btnClickable();
        assignProductOrderReceivePresenter.assignProductOrderReceive();
    }

    private void btnClickable() {
        if (isClick) {
            btn_receive_order.setClickable(false);
            btn_receive_order.setBackgroundResource(R.drawable.btn_green_submit);
            btn_return_order.setClickable(false);
            btn_return_order.setBackgroundResource(R.drawable.btn_red_delete);
        } else {
            btn_receive_order.setClickable(false);
            btn_receive_order.setBackgroundResource(R.mipmap.btn_long_bg_gray);
            btn_return_order.setClickable(false);
            btn_return_order.setBackgroundResource(R.mipmap.btn_long_bg_gray);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_arrow_down:
                if (ll_content.getVisibility() == View.VISIBLE) {
                    ll_content.setVisibility(View.GONE);
                    img_arrow_down.setBackgroundResource(R.mipmap.arrow_down_icon);
                } else {
                    ll_content.setVisibility(View.VISIBLE);
                    img_arrow_down.setBackgroundResource(R.mipmap.arrow_top);
                }
                break;
            // 接收工单
            case R.id.btn_receive_order:
                if (isClick)
                    receiveOrder();
                break;
            // 退回工单
            case R.id.btn_return_order:
                if (isClick)
                    returnOrder();
                break;
        }
    }

    @Override
    public String getOrderId() {
        return workPartBean.getOrderId();
    }

    @Override
    public String getProductId() {
        return workPartBean.getProductId();
    }

    @Override
    public CommonEnum.STATUS getStatus() {
        return status;
    }

    @Override
    public String getWorkPartId() {
        return workPartBean.getWorkPartId();
    }

    @Override
    public String getWorkPartOrderId() {
        return workPartOrderBean.getWorkPartOrderId();
    }

    @Override
    public void assignProductOrderReceiveSuccessed() {
        if (null != status) {
            if (status == CommonEnum.STATUS.RECEIVED)
                Toast.makeText(getActivity(), "接收成功！", Toast.LENGTH_SHORT).show();
            else if (status == CommonEnum.STATUS.RETURNED)
                Toast.makeText(getActivity(), "退回成功！", Toast.LENGTH_SHORT).show();
        }
        EventBus.getDefault().post(new ProduceForTeamEvent());
        isClick = false;
        btnClickable();
    }

    @Override
    public void assignProductOrderReceiveFailed(final String desc) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                isClick = true;
                btnClickable();
                Toast.makeText(getActivity(), desc, Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
